from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QWidget
import sys
from PySide2.QtCharts import QtCharts
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QHBoxLayout
import openpyxl

class MyChartWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.chart = QtCharts.QChart()
        self.view = QtCharts.QChartView(self.chart)
        self.series = QtCharts.QHorizontalBarSeries()
        self.axis = QtCharts.QBarCategoryAxis()

        self.wb = openpyxl.load_workbook(
            r'C:\Users\zuozh\Desktop\太原科大Python综合培训\国家数据\就业人员和工资\按经济类型分乡村就业人员.xlsx'
        )
        self.ws = self.wb['年度数据']

        self.__dataInit()

        # 布局
        layout = QHBoxLayout()
        layout.addWidget(self.view)
        self.setLayout(layout)

    def __dataInit(self):
        # barset = QtCharts.QBarSet('A')
        # barset.append(3)
        # barset.append(5)
        # barset.append(6)
        # self.series.append(barset)
        #
        # barset2 = QtCharts.QBarSet('B')
        # barset2.append(8)
        # barset2.append(2)
        # barset2.append(6)
        # self.series.append(barset2)
        for i in range(4, 7):
            barset = QtCharts.QBarSet(
                str(
                    self.ws.cell(row=i, column=1).value
                )
            )
            for j in range(2, 10):
                barset.append(
                    int(
                        self.ws.cell(row=i, column=j).value
                    )
                )
            self.series.append(barset)


        nameList = list()
        # nameList.append('甲')
        # nameList.append('乙')
        # nameList.append('丙')
        for i in range(2, 10):
            nameList.append(
                str(
                    self.ws.cell(row=3, column=i).value
                )
            )
        self.axis.append(nameList)

        self.chart.addAxis(self.axis, Qt.AlignLeft)
        self.chart.setAxisY(self.axis,
                            self.series)


        self.chart.addSeries(self.series)
        self.chart.setTitle('条图示例')


if __name__ == "__main__":
    app = QApplication()

    mcw = MyChartWidget()
    mcw.show()

    sys.exit(app.exec_())